package com.cnpc.iot.scgl.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnpc.framework.annotation.RefreshCSRFToken;
import com.cnpc.framework.annotation.VerifyCSRFToken;
import com.cnpc.framework.base.pojo.Result;
import com.cnpc.framework.base.service.BaseService;
import com.cnpc.framework.utils.StrUtil;
import com.cnpc.iot.scene.entity.Device;
import com.cnpc.iot.scgl.entity.Camera;
import com.cnpc.iot.video.service.VideoService;

/**
* 摄像头管理控制器
* @author jrn
* 2018-05-02 20:48:29由代码生成器自动生成
*/
@Controller
@RequestMapping("/camera")
public class CameraController {

    @Resource
    private BaseService baseService;
    
    @Resource
    private VideoService videoService;

    @RequestMapping(value="/list",method = RequestMethod.GET)
    public String list(){
        return "iot/scgl/camera_list";
    }
    
    @RequestMapping(value="/showVideo",method = RequestMethod.GET)
    public String videoList(){
        return "iot/scgl/show_video";
    }

    @RefreshCSRFToken
    @RequestMapping(value="/edit",method = RequestMethod.GET)
    public String edit(String id,HttpServletRequest request){
        request.setAttribute("id", id);
        return "iot/scgl/camera_edit";
    }

    @RequestMapping(value="/detail",method = RequestMethod.GET)
    public String detail(String id,HttpServletRequest request){
        request.setAttribute("id", id);
        return "iot/scgl/camera_detail";
    }

    @RequestMapping(value="/get/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Camera get(@PathVariable("id") String id){
        return baseService.get(Camera.class, id);
    }

    @VerifyCSRFToken
    @RequestMapping(value="/save")
    @ResponseBody
    public Result save(Camera camera){
        if(StrUtil.isEmpty(camera.getId())){
            baseService.save(camera);
        }
        else{
            Camera camera_old=baseService.get(Camera.class,camera.getId());
            BeanUtils.copyProperties(camera,camera_old, "remark");
            camera_old.setUpdateDateTime(new Date());
            baseService.update(camera_old);
        }
        return new Result(true);
    }



    @RequestMapping(value="/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable("id") String id){
        Camera camera=this.get(id);
        try{
            baseService.delete(camera);
            return new Result();
        }
        catch(Exception e){
            return new Result(false,"该数据已经被引用，不可删除");
        }
    }

    /**
     * 数据采集视频直播
     * @param deviceId
     * @param request
     * @return
     */
    @RequestMapping(value = "/video/{deviceId}", method = RequestMethod.GET)
	public String video(@PathVariable("deviceId") String deviceId, HttpServletRequest request) {
		Device device = baseService.get(Device.class, deviceId);
		Camera camera = null;
		if(null != device) {
			List<Camera> cameras = baseService.find("from Camera where deviceSerial='"+device.getPort()+"'");
			if(null != cameras && cameras.size() > 0) {
				camera = cameras.get(0);
				request.setAttribute("video", camera);
			}
		}
		if(camera != null && camera.getCameraType().equals("1")) {
			return "iot/scgl/camera_video";
		}else {
			return "iot/scgl/camera_video2";
		}
		
	}
    
    /**
     * 九宫格列表
     * @param cameraId
     * @param request
     * @return
     */
    @RequestMapping(value = "/videoShow/{cameraId}", method = RequestMethod.GET)
	public String videoShow(@PathVariable("cameraId") String cameraId, HttpServletRequest request) {
    	Camera camera= baseService.get(Camera.class, cameraId);
		if(null != camera) {
			request.setAttribute("video", camera);
		}
		return "iot/scgl/camera_video2";
	}
    
    /**
     * 九宫格列表点击放大
     * @param cameraId
     * @param request
     * @return
     */
    @RequestMapping(value = "/openVideos/{cameraId}", method = RequestMethod.GET)
	public String openVideos(@PathVariable("cameraId") String cameraId, HttpServletRequest request) {
    	Camera camera= baseService.get(Camera.class, cameraId);
		if(null != camera) {
			request.setAttribute("video", camera);
		}
		if(camera.getCameraType().equals("1")) {
			return "iot/scgl/camera_video3";
		}else {
			return "iot/scgl/camera_video4";
		}
	}
    
    @RequestMapping(value = "/start/{cameraId}/{direction}", method = RequestMethod.GET)
    @ResponseBody
	public Result startControl(@PathVariable("cameraId") String cameraId,@PathVariable("direction") String direction, HttpServletRequest request) {
    	Camera camera = baseService.get(Camera.class, cameraId);
		if(null != camera) {
			String token = videoService.getAccessToken(camera.getAppKey(), camera.getAppSecret());
			if(!StringUtils.isEmpty(token)) {
				boolean flag = videoService.startYunTaiCont(token, camera.getDeviceSerial(), camera.getChannelNo(), Integer.valueOf(direction), 1);
				if(flag) {
					return new Result();
				}
			}
		}
		return new Result(false);
	}
    
    @RequestMapping(value = "/stop/{cameraId}/{direction}", method = RequestMethod.GET)
    @ResponseBody
	public Result stopControl(@PathVariable("cameraId") String cameraId,@PathVariable("direction") String direction, HttpServletRequest request) {
    	Camera camera = baseService.get(Camera.class, cameraId);
		if(null != camera) {
			String token = videoService.getAccessToken(camera.getAppKey(), camera.getAppSecret());
			if(!StringUtils.isEmpty(token)) {
				boolean flag = videoService.stopYunTaiCont(token, camera.getDeviceSerial(), camera.getChannelNo(), Integer.valueOf(direction));
				if(flag) {
					return new Result();
				}
			}
		}
		return new Result(false);
	}


    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    @ResponseBody
    public Result getAllCamera() {
    	List<Camera> list = baseService.find("from Camera");
    	Result result = new Result();
    	result.setData(list);
    	return result;
    }
    
    
}
